package com.ctrl.riveradministrator.db;


import com.ctrl.riveradministrator.mvp.model.CurrentLocation;

import org.greenrobot.greendao.query.QueryBuilder;

import java.util.List;

import static com.ctrl.riveradministrator.RaApplication.raApplication;

public class SQLiteUtils {

    private static SQLiteUtils instance;
    CurrentLocationDao currentLocationDao;
    DaoSession daoSession;

    private SQLiteUtils() {
        currentLocationDao = raApplication.getDaoSession().getCurrentLocationDao();
        daoSession = raApplication.getDaoSession();
    }

    public static SQLiteUtils getInstance() {
        if (instance == null) {
            instance = new SQLiteUtils();
        }
        return instance;
    }


    //增加
    public void addContacts(CurrentLocation currentLocation) {
        currentLocationDao.insert(currentLocation);
    }

    //删除
    public void deleteContacts(CurrentLocation currentLocation) {
        currentLocationDao.delete(currentLocation);
    }

    //修改
    public void updateContacts(CurrentLocation currentLocation) {
        currentLocationDao.update(currentLocation);
    }

    public List queryAll() {
        List<CurrentLocation> currentLocations = daoSession.loadAll(CurrentLocation.class);
        return currentLocations;
    }

    public List queryBlack(int offset) {
        QueryBuilder qb = daoSession.queryBuilder(CurrentLocation.class);
        return qb.offset(offset * 200).limit(200).list();
    }

    //删除表中内容
    public void deleteAllContact() {
        currentLocationDao.deleteAll();
    }
}
